#include "ram.h"

void ram::init(void)
{
  std::string buf;
  ifstream f;

  cout << "RAM initialization... ";

  f.open("mem.dat", ios::in);

  for (int i = 0; i < M_BLOCKS; i++) {
    getline(f, buf);
    _mem[i] = buf.c_str();
  }

  f.close();

  cout << "Complete!\n";
}

void ram::compute(void)
{
  while (true) {

    wait();

    if (cs.read() == SC_LOGIC_1) { 

      _m_addr.range(31, 30) = 0;
      _m_addr.range(29,  2) = addr.read()(31, 4);
      _m_addr.range( 1,  0) = 0;

      if (rw.read() == SC_LOGIC_0) {

        rdy.write(SC_LOGIC_0);
        wait(100, SC_NS); // reading four words
        rdy.write(SC_LOGIC_1);

        dout0.write(_mem[ADDR0]);
        dout1.write(_mem[ADDR1]);
        dout2.write(_mem[ADDR2]);
        dout3.write(_mem[ADDR3]);
        
      } else if (rw.read() == SC_LOGIC_1) {

        rdy.write(SC_LOGIC_0);
        wait(100, SC_NS); // writing four words
        rdy.write(SC_LOGIC_1);

        _mem[ADDR0] = din0.read();
        _mem[ADDR1] = din1.read();
        _mem[ADDR2] = din2.read();
        _mem[ADDR3] = din3.read();

      }

    }
  }
}
